package com.example.network.HttpURLConnection;

import android.text.TextUtils;
import android.util.Log;

import com.example.network.entity.NameValuePair;
import com.example.network.tool.HttpURLConnectionTool;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class UrlConnManager {

    public static void postParams(OutputStream output, List<NameValuePair> paramsList) throws IOException {
        StringBuilder mStringBuilder = new StringBuilder();

        for (NameValuePair pair : paramsList) {
            if (!TextUtils.isEmpty(mStringBuilder)) {
                mStringBuilder.append("&");
            }
            mStringBuilder.append(URLEncoder.encode(pair.getName(), "UTF-8"));
            mStringBuilder.append("=");
            mStringBuilder.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
        }
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, "UTF-8"));
        writer.write(mStringBuilder.toString());
        writer.flush();
        writer.close();
    }

    private void useHttpUrlConnectionPost(String url) {
        InputStream mInputStream = null;
        HttpURLConnection mHttpURLConnection = HttpURLConnectionTool.getHttpURLConnection(url, "POST");
        try {
            List<NameValuePair> postParams = new ArrayList<>();
            //要传递的参数
            postParams.add(new NameValuePair("username", "moon"));
            postParams.add(new NameValuePair("password", "123"));
            //HttpURLConnection.getOutputStream输出流 请求
            UrlConnManager.postParams(mHttpURLConnection.getOutputStream(), postParams);
            //HttpURLConnection.connect 连接
            mHttpURLConnection.connect();
            //HttpURLConnection.getInputStream输入流 响应
            mInputStream = mHttpURLConnection.getInputStream();
            int code = mHttpURLConnection.getResponseCode();
            String respose = HttpURLConnectionTool.converStreamToString(mInputStream);
            Log.i("wangshu", "请求状态码:" + code + "\n请求结果:\n" + respose);
            mInputStream.close();
            mHttpURLConnection.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static void useHttpUrlConnectionGet(String path) {
        InputStream mInputStream = null;
        List<NameValuePair> postParams = new ArrayList<>();
        //要传递的参数
        postParams.add(new NameValuePair("username", "moon"));
        postParams.add(new NameValuePair("password", "123"));
        //GET请求直接在链接后面拼上请求参数
        StringBuffer mPath = new StringBuffer(path + "?");
        for (NameValuePair postParam : postParams) {
            mPath.append(postParam.getName() + "=" + postParam.getValue() + "&");
        }
        HttpURLConnection mHttpURLConnection = HttpURLConnectionTool.getHttpURLConnection(mPath.toString(), "GET");
        try {

            //HttpURLConnection.connect 连接
            mHttpURLConnection.connect();
            //HttpURLConnection.getInputStream输入流 响应
            mInputStream = mHttpURLConnection.getInputStream();
            int code = mHttpURLConnection.getResponseCode();
            String respose = HttpURLConnectionTool.converStreamToString(mInputStream);
            Log.i("wangshu", "请求状态码:" + code + "\n请求结果:\n" + respose);
            mHttpURLConnection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //关闭输入流
        finally {
            try {
                if (mInputStream != null) {
                    mInputStream.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }


    public Thread useHttpUrlConnectionGetThread_post() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                useHttpUrlConnectionPost("http://www.baidu.com");
            }
        });


        thread.start();
        return thread;
    }

    public Thread useHttpUrlConnectionGetThread_get() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                useHttpUrlConnectionGet("http://www.baidu.com");
            }
        });


        thread.start();
        return thread;
    }
}
